<template>
    <div class="goodsInfo-container">
        <!-- 商品轮播 -->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <swiper :swiperInfo="lunBo" :isfull="false"></swiper>
                </div>
            </div>
        </div>

        <!-- 商品购买 -->
        <div class="mui-card">
            <div class="mui-card-header">{{ goods.title }}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        市场价:<del>￥{{ goods.market_price }}</del>&nbsp;
                        销售价:<span>￥{{ goods.sell_price }}</span>
                    </p>
                    <p>
                        购买数量：<num-box
                                    @getcount="getSelectedCount"
                                    :maxcount="goods.stock_quantity"
                                ></num-box>

                    </p>
                    <p>
                        <mt-button type="primary" size="small">立即购买</mt-button>
                        <mt-button type="danger" size="small" @click="addCart()">加入购物车</mt-button>
                    </p>
                </div>
            </div>
        </div>

        <!-- 商品参数 -->
        <div class="mui-card">
            <div class="mui-card-header">商品参数</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品货号：{{ goods.goods_no }}</p>
                    <p>库存情况：{{ goods.stock_quantity }}件</p>
                    <p>上架时间：{{ goods.add_time | dateFormat }}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain @click="goDesc(id)">图文介绍</mt-button>
                <mt-button type="danger" size="large" plain @click="goComment(id)">商品评论</mt-button>
            </div>
        </div>

        <transition
            @before-enter="beforeEnter"
            @enter="enter"
            @after-enter="afterEnter">
            <div class="ball" v-show="ballFlag" ref="ball"></div>
        </transition>

    </div>
</template>

<script>
    import { Toast } from 'mint-ui'
    import Swiper from '../../../../components/common/swiper/Swiper.vue'
    import NumBox from './HomeGoodsInfo_numbox.vue'

    export default {
        name: "",
        data(){
            return {
                id: this.$route.params.id,
                lunBo: [],
                goods: {},
                ballFlag: false, //控制小球的隐藏和显示
                selectedCount: 1
            }
        },
        created(){
            this.getGoodsThum()
            this.getGoodsInfo()
        },
        methods: {
            getGoodsThum(){
                this.$http.get('static/data/goodsthumimages.json').then(res => {
                    if(res.body.status === 0){
                        var ret = res.body.message.find(item => item.id===parseInt(this.id))
                        if(!ret) return Toast('图片获取失败')
                        ret.images.forEach(item => {
                            item.img = item.src
                        })
                        this.lunBo = ret.images
                    }
                })
            },
            getGoodsInfo(){
                this.$http.get('static/data/goodsInfo.json').then(res => {
                    if(res.body.status === 0){
                        var ret = res.body.message.find(item => item.id===parseInt(this.id))
                        if(!ret) return Toast('商品信息获取失败')
                        this.goods = ret.goods
                    }
                })
            },
            goDesc(id){
                this.$router.push({ name: 'goodsdesc', params: { id } })
            },
            goComment(id){
                this.$router.push({ name: 'goodscomment', params: { id } })
            },
            addCart(){ //加入购物车
                this.ballFlag = !this.ballFlag

                this.$store.commit('addCar', {
                    id: parseInt(this.id),
                    count: this.selectedCount,
                    price: this.goods.sell_price,
                    selected: true,
                    max: this.goods.stock_quantity
                })
            },

            beforeEnter(el){
                el.style.transform = "translate(0, 0)"
            },
            enter(el, done){
                el.offsetWidth
                // 使用徽标的横纵坐标 - 小球的横纵坐标 = 要位移的横纵值

                // 获取去小球在页面中的位置
                let ballPosition = this.$refs.ball.getBoundingClientRect()
                // 获取徽标的位置
                let badgePosition = document.getElementById('badge').getBoundingClientRect()
                let x = badgePosition.left - ballPosition.left
                let y = badgePosition.top - ballPosition.top

                el.style.transform = "translate("+ x +"px, "+ y +"px)" //终点
                el.style.transition = "all 0.6s cubic-bezier(.71,-0.36,.1,.19)"

                done() // 等于 afterEnter 的执行调用
            },
            afterEnter(el){
                this.ballFlag = !this.ballFlag
            },
            getSelectedCount(count){
                // 当子组件把选中的数量传递给父组件时，把选中的值保存到data中
                this.selectedCount = count
            }
        },
        components: {
            Swiper,
            NumBox
        }
    }
</script>

<style lang="less" scoped>
.goodsInfo-container{
    background-color: #eee;
    overflow: hidden;
    .price{
        span{
            font-size: 16px;
            color: red;
            font-weight: bold;
        }
    }
    p:last-of-type{
        margin: 0;
    }
    .mui-card-footer{
        display: block;
        button{
            margin: 10px 0;
        }
    }
    .ball{
        height: 14px;
        width: 14px;
        border-radius: 50%;
        background-color: red;
        position: absolute;
        top: 390px;
        left: 146px;
        z-index: 99;
    }
}
</style>